﻿using System;

namespace Segmentation.Business.Criterions
{
    public class Quality : Criterion
    {
        #region Properties

        public override string Name
        {
            get
            {
                return "Quality";
            }
        }

        #endregion

        #region Methods

        protected override double Calculate(byte[,] mapIdeal, byte[,] mapCalculated)
        {
            int height = mapIdeal.GetLength(0);
            int width = mapIdeal.GetLength(1);

            int P = 0;
            int F = 0;

            for (int y = 0; y < height; y++)
            {
                int xContourCalculated = -1;
                int xContourIdeal = -1;
                for (int x = 0; x < width - 1; x++)
                {
                    if (mapCalculated[y, x] != mapCalculated[y, x + 1])
                    {
                        xContourCalculated = x;
                        P++;
                    }
                    if (mapIdeal[y, x] != mapIdeal[y, x + 1])
                    {
                        xContourIdeal = x;
                    }
                }
                if ((xContourCalculated - xContourIdeal) != 0)
                {
                    F++;
                }
            }

            return Math.Sqrt(F) / P;
        }

        #endregion
    }
}
